-- UIWeaponsExplain
-- Create by luohj Jan/28/2015
-- 武器/徽章说明界面

UIWeaponsExplain = class("UIWeaponsExplain",function()
    return cc.Layer:create();
end
);

function UIWeaponsExplain.create(classId)
    return UIWeaponsExplain.new(classId);
end

-- 构造函数
function UIWeaponsExplain:ctor(classId)
    -- 初始化
    self:setName("UIWeaponsExplain");

    local node = cc.CSLoader:createNode("layout/equip/WeaponsExplain.csb");
    self:addChild(node);
    self.node = node;
    local CT = node:getChildByName("CT");

    -- 设置显示的层级
    self:setLocalZOrder(UIMgr.TOP_MENU_ZORDER + 1);

    self.classId = classId;

    self.CT = CT;

    -- 注册点击事件
    self:registerTouchEvent();

    self:initView();

    -- 适配
    self:resize(node);
end

function UIWeaponsExplain:initView()
    -- 设置装备名字
    local nameLabel = findChildByName(self.CT, "name");
    local name = ItemM.query(self.classId, "name");
    TextStyleM.setTextStyle(nameLabel, TextStyleM.TEXT_SIZE_MIDDLE, TextStyleM.TEXT_COLOR_DARK_RED);
    nameLabel:setString(name);

    -- 设置道具图标
    local iconIndex = ItemM.query(self.classId, "icon");

    -- 获取道具图标控件
    local equipImage = findChildByName(self.CT, "node1/weapon_image");
    equipImage:loadTexture(getItemIconPath(iconIndex));

    -- 显示阶级
    local rank = EquipM.query(self.classId, "rank");
    if rank then
        local petRankNode = findChildByName(self.CT, "star_node");
        local rankLayer = createStarLayer(rank, "images/ui/symbol/star_little.png");
        if rankLayer then
            petRankNode:removeAllChildren();
            petRankNode:addChild(rankLayer);
        end
    end

    -- 设置装备效果
    local effectLabel = findChildByName(self.CT, "desc");
    local effect = "";
    local props = EquipM.query(self.classId).prop;
    for i = 1, #props do
        if props[i][1] == 200 and i + 1 <= #props and
           props[i+1][1] == 200 then
            effect = effect .. PropM.getPropDesc(props[i]) .. " ";
        else
            effect = effect .. PropM.getPropDesc(props[i]) .. "\n";
        end
    end
    TextStyleM.setTextStyle(effectLabel, TextStyleM.TEXT_SIZE_SMALL, TextStyleM.TEXT_COLOR_BLUE);
    applyString(effectLabel, effect);
end

--适配
function UIWeaponsExplain:resize(node)
    -- bg居中显示并充满整个屏幕
    AlignM.fitToScreen(node, "bg");

    -- CT居中并缩放至合适大小
    AlignM.alignToCenter(node, "CT");

    -- BT置于底部并缩放至合适大小
    AlignM.alignToBottomCenter(node, "BT");
end

-- 注册点击事件
function UIWeaponsExplain:registerTouchEvent()
    -- 注册返回按钮点击事件
    local btnBack = findChildByName(self.node, "BT/btn_back");
    local function onBackClick(sender, eventType)
        if eventType == ccui.TouchEventType.ended then
            -- 关闭当前界面
            AudioM.playFx("button_return");
            UIMgr.getCurrentScene():removeFormByName("UIWeaponsExplain");
        end
    end
    AddBackEventHandler(self, btnBack, onBackClick);
    TextStyleM.setTextStyle(btnBack, TextStyleM.TEXT_SIZE_MIDDLE, TextStyleM.TEXT_COLOR_BROWN_YELLOW, true);
    btnBack:setTitleText(getLocStr("btn_text_back"));

    -- 注册背景点击事件
    local btnBgBack = findChildByName(self.node, "bg/bgImg");
    local function onBgBackClick(sender, eventType)
        if eventType == ccui.TouchEventType.ended then
            -- 关闭当前界面
            AudioM.playFx("button_return");
            UIMgr.getCurrentScene():removeFormByName("UIWeaponsExplain");
        end
    end
    btnBgBack:addTouchEventListener(onBgBackClick);

end
